package com.yogee.topseller.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.base.HttpActivity;
import com.yogee.topseller.activity.login.model.bean.InterestLabelBean;
import com.yogee.topseller.adapter.InterestLabelNewAdapter;
import com.yogee.topseller.adapter.InterestLabelSxAdapter;
import com.yogee.topseller.bean.MySelfInfo;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.layout.TitleLayout;
import com.yogee.topseller.utils.LogUtils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/12/5.
 */

public class SearchByConditionsActivity extends HttpActivity {

    @BindView(R.id.search_lable_rv)
    RecyclerView lableRv;
    @BindView(R.id.add_card_titlelayout)
    TitleLayout titlelayout;
    private List<InterestLabelBean.ListBean> lableBeen;
    private InterestLabelBean interestLabelBean;
    public  static List<InterestLabelBean.ListBean> allLablebean;
    private InterestLabelSxAdapter mAdapter;

    private String yjjs="",tzcs="",tzqx="",cplx="",fxjg="",diqu="",tzly="";

    public List<InterestLabelBean.ListBean> getBean(){
        return allLablebean;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_by_conditions;
    }

    @Override
    protected void initView() {
        titlelayout.setActivity(this);
        titlelayout.setTitle("条件筛选");
        lableRv.setLayoutManager(new LinearLayoutManager(this));
        lableRv.setHasFixedSize(true);
        mAdapter = new InterestLabelSxAdapter(this);
        lableRv.setAdapter(mAdapter);
        searchFilter();
    }

    private void searchFilter() {
        HttpManager.getInstance().searchFilter().compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<InterestLabelBean>() {
            @Override
            public void onNext(InterestLabelBean o) {
                interestLabelBean = o;
                interestLabelBean.setList1(o.getList());
                MySelfInfo.getInstance().setAllLablebean(o.getList());
                lableBeen = o.getList();
                mAdapter.setCity(o);
                mAdapter.addData(lableBeen);

            }
        }, this));
    }

    @OnClick({R.id.search_lable_reset_tv,R.id.search_lable_sure_tv})
    public void onViewClicked(View view) {
        switch(view.getId()){
            case R.id.search_lable_reset_tv:
                mAdapter = new InterestLabelSxAdapter(SearchByConditionsActivity.this);
                lableRv.setAdapter(mAdapter);
                mAdapter.addData(lableBeen);
                mAdapter.setCity(interestLabelBean);
                break;
            case R.id.search_lable_sure_tv:
                if(mAdapter.checkValue()){
                    String name = "";
                    List<InterestLabelBean.ListBean> mSelect = mAdapter.getmSelectDatas();
                    if(mSelect!=null&&mSelect.size()>0){
                        for(int i = 0; i<mSelect.size();i++){
                            if(i==0){
                                yjjs = "";
                                if(mSelect.get(i).getLabelList().size()>0){
                                    for(int j=0;j<mSelect.get(i).getLabelList().size();j++){
                                        if(j==0)
                                            yjjs += mSelect.get(i).getLabelList().get(j).getName();
                                        else{
                                            yjjs += ","+mSelect.get(i).getLabelList().get(j).getName();
                                        }
                                    }
                                }
                            }
                            else if(i==1){
                                tzcs = "";
                                tzcs = mSelect.get(i).getType();
                            }
                            else if(i==2){
                                tzqx = "";
                                if(mSelect.get(i).getLabelList().size()>0){
                                    for(int j=0;j<mSelect.get(i).getLabelList().size();j++){
                                        if(j==0)
                                            tzqx += mSelect.get(i).getLabelList().get(j).getName();
                                        else{
                                            tzqx += ","+mSelect.get(i).getLabelList().get(j).getName();
                                        }
                                    }
                                }
                            }else if(i==3){
                                cplx = "";
                                if(mSelect.get(i).getLabelList().size()>0){
                                    for(int j=0;j<mSelect.get(i).getLabelList().size();j++){
                                        if(j==0)
                                            cplx += mSelect.get(i).getLabelList().get(j).getName();
                                        else{
                                            cplx += ","+mSelect.get(i).getLabelList().get(j).getName();
                                        }
                                    }
                                }
                            }else if(i==4){
                                fxjg="";
                                if(mSelect.get(i).getLabelList().size()>0){
                                    for(int j=0;j<mSelect.get(i).getLabelList().size();j++){
                                        if(j==0)
                                            fxjg += mSelect.get(i).getLabelList().get(j).getName();
                                        else{
                                            fxjg += ","+mSelect.get(i).getLabelList().get(j).getName();
                                        }
                                    }
                                }
                            }else if(i==5){
                                diqu = "";
                                if(mSelect.get(i).getLabelList().size()>0){
                                    for(int j=0;j<mSelect.get(i).getLabelList().size();j++){
                                        if(j==0)
                                            diqu += mSelect.get(i).getLabelList().get(j).getName();
                                        else{
                                            diqu += ","+mSelect.get(i).getLabelList().get(j).getName();
                                        }
                                    }
                                }
                            }else if(i==6){
                                tzly = "";
                                if(mSelect.get(i).getLabelList().size()>0){
                                    for(int j=0;j<mSelect.get(i).getLabelList().size();j++){
                                        if(j==0)
                                            tzly += mSelect.get(i).getLabelList().get(j).getName();
                                        else{
                                            tzly += ","+mSelect.get(i).getLabelList().get(j).getName();
                                        }
                                    }
                                }
                            }
                        }
                    }

                    searchIndex(yjjs,tzcs,tzqx,cplx,fxjg,diqu,tzly);
                    break;
                }

        }

    }

    private void searchIndex(String yjjs,String tzcs,String tzqx,String cplx,String fxjg,String diqu,String tzly) {
        startActivity(new Intent(SearchByConditionsActivity.this, SearchResultActivity.class)
                .putExtra("yjjs", yjjs)
                .putExtra("tzcs", tzcs)
                .putExtra("tzqx", tzqx)
                .putExtra("cplx", cplx)
                .putExtra("fxjg", fxjg)
                .putExtra("diqu", diqu)
                .putExtra("tzly", tzly)
                .putExtra("type","1"));
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode){
            case 1:
                Bundle b = data.getExtras();
                InterestLabelBean.ListBean beans = (InterestLabelBean.ListBean) b.getSerializable("selectlist");
                int position = data.getIntExtra("position",0);
                LogUtils.e("postiton","postiton:"+position+"");
                mAdapter.setSelectItem(position,beans.getLabelList());
//                lableBeen.get(position).setLabelList(beans.getLabelList());
                break;
            default:
                break;
        }
    }
}
